home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest_04.lha / src / scheduler.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-06  |  2.5 KB  |  109 lines

  1.  
  2. //
  3. // Scheduler object where all the grungy details
  4. // are kept.
  5. //
  6.  
  7.  
  8.  
  9.  
  10.  
  11. //
  12. // All ops on base ThreadPool are "does-not-understand"
  13. //
  14. class ThreadPool    {
  15. public:
  16.     ThreadPool();
  17.     virtual ~ThreadPool();
  18.     virtual Thread*    get();
  19.     virtual void    insert(Thread*);
  20.     virtual int    size();
  21.     int        empty()
  22.         { return size() == 0; }
  23. };
  24.  
  25.  
  26.  
  27. //
  28. // Derive public to allow coersion
  29. //
  30. class ThreadPoolQueue : public ThreadPool    {    
  31.     ThreadQ    *tp_tq;
  32. public:
  33.     ThreadPoolQueue();
  34.     ~ThreadPoolQueue();
  35.     Thread    *get();
  36.     void    insert(Thread*);
  37.     int    size();
  38. };
  39.  
  40.  
  41.  
  42.  
  43. class Processor;
  44. struct sigcontext;
  45.  
  46.  
  47. #define DEFQUANTUM    0        /* milliseconds */
  48.  
  49. class Scheduler  : public Object    {
  50. protected:
  51.     ThreadPool    *sc_t_ready;        // threads wanting to run
  52.     Process*    sc_p_procs[NUMPROCS];    // workers live here
  53.     int        sc_p_numschedulers;    // max# which can be active
  54.     int        sc_p_activeschedulers;    // # which are active
  55.     int        sc_p_busybits;        // 32 is the proc limit
  56.     Spinlock    *sc_lock;        // Lock busybits
  57.     inline int      busybits(int on);    // atomic set
  58.     int        sc_quantum;        // in ms. if 0, no preemption
  59. public:
  60.     Scheduler(int nschedulers, int quantum = DEFQUANTUM);
  61.     virtual ~Scheduler();
  62.     virtual Thread* getreadythread();    
  63.     virtual void resume(Thread* t);            // put t back on ready q
  64.     virtual int invoke();                // yeah yeah yeah...
  65.     void begin(Thread* t)            // start t running somewhere
  66.         { resume(t); }
  67.     virtual void halt();            // come to a crashing stop! nicely
  68.     virtual void abort(int sigself);      // not so nicely (may core dump)
  69. //    virtual void kill(Thread* t);            // nuke t
  70.     virtual void    error(char *s);        
  71.     virtual void    print(ostream& = cout);
  72.     int    readyqlen()
  73.         { return sc_t_ready->size(); }
  74.     int    quantum()
  75.         { return sc_quantum; }
  76.     int    pidtoprocnum(int p);
  77.     void    storecore(int pnum);        // rename a core file
  78.     virtual ThreadPool*    getreadypool();
  79.     virtual ThreadPool*    setreadypool(ThreadPool*);// use new, return old
  80.     virtual void initsighandlers(int setupdefaults);
  81. friend    sigpreempt_alrm(int sig, int code, sigcontext *scp);
  82. friend  schedulerSigHandler(int sig, int code, sigcontext *scp);
  83. friend  schedulerReapChild(int sig, int code, sigcontext *scp);
  84. };
  85.  
  86.  
  87. //
  88. // Keep track of who's busy and who's not.  Access routine must do
  89. // the locking.
  90. //
  91. inline int
  92. Scheduler::busybits(int on)
  93. {
  94.     register int procmask = 1<<(thisproc->id());
  95.     register int shadowbits;
  96.     
  97. //    sc_lock->lock();    
  98.     if (on)
  99.         shadowbits = sc_p_busybits |= procmask;
  100.     else
  101.         shadowbits = sc_p_busybits &= ~procmask;
  102. //    sc_lock->unlock();        
  103.     return shadowbits;
  104. }
  105.  
  106. extern Scheduler    *sched;
  107.  
  108.  
  109.